package com.lxw.station.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxw.mutils.DateUtil;
import com.lxw.station.components.CsdnService;
import com.lxw.station.dto.SourceDto;
import com.lxw.station.entity.Downloads;
import com.lxw.station.service.DownloadsService;
import com.lxw.station.service.RestTemplateService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

@Component
@Slf4j
public class CsdnTask4FixedDelay {
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    DownloadsService downloadsService;
    @Resource
    CsdnService csdnService;
    Set<String> set = new HashSet<>();

    @Scheduled(cron = "0 0 1 * * ?")
    public void clean() {
        set = new HashSet<>();
    }

    /**
     * 获取下载了的资源
     */
//    @Scheduled(fixedDelay = 60 * 1000)
    public void get_pay_log_banner() {
        String date = DateUtil.Date2ShortString(new Date());
        if (CollectionUtils.isEmpty(set)) {
            List<Downloads> list = downloadsService.list(new QueryWrapper<>(new Downloads().setDate(date)));
            if (!CollectionUtils.isEmpty(list)) {
                for (Downloads downloads : list) {
                    set.add(downloads.getUrl());
                }
            }
        }
        String url = "https://download.csdn.net/source/get_pay_log_banner";
        String exchange = RestTemplateService.exchange(url, HttpMethod.GET, null, null);
        List<Downloads> downloads = JSON.parseObject(exchange).getJSONArray("data").toJavaList(Downloads.class);
        for (Downloads download : downloads) {
            try {
                if (set.add(download.getUrl())) {
                    download.setDate(date);
                    downloadsService.save(download);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读lzw下载资源
     */
    @SneakyThrows
//    @Scheduled(fixedDelay = 60 * 60 * 1000)
    public void sourceRead() {
        Object o = redisTemplate.opsForValue().get("csdn:lzw:source");
        System.out.println("o = " + o);
        JSON.parseArray(o.toString()).toJavaList(SourceDto.class);
//        List<SourceDto> list = CSDNApi.sourceList("baidu_16992441");
        List<SourceDto> list = JSON.parseArray(o.toString()).toJavaList(SourceDto.class);
        for (SourceDto sourceDto : list) {
            String url = sourceDto.getUrl();
            String exchange = RestTemplateService.exchange(url, HttpMethod.GET);
            Random random = new Random();
            int m = random.nextInt(60);
            log.info("sourceRead,即将等待{}秒", m);
            Thread.sleep(m * 1000);
        }
    }

    /**
     * 取消关注
     */
    @SneakyThrows
//    @Scheduled(fixedDelay = 8*60 * 60 * 1000)
    public void unFollows() {
        csdnService.unFollows();
    }
}
